﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkFlowCore.Plugins;

namespace WorkFlowCore.Host.ViewModels.Plugins
{
    /// <summary>
    /// 插件应用配置信息
    /// </summary>
    public class PluginApplyConfigDto
    {
        /// <summary>
        /// 配置列表
        /// </summary>
        public PluginsTypeConfig[] Configs { get; set; }
        public List<PluginApplyConfig> ToPluginApplyConfig()
        {
            var pluginApplyConfigs = new List<PluginApplyConfig>();
            foreach (var config in Configs)
            {
                var configItems = config.ConfigItems;
                foreach (var item in configItems)
                {
                    pluginApplyConfigs.Add(new PluginApplyConfig(item.Id, item.Name, config.ExecutableTypeFullName, item.EntryFullName, item.ConfigValue, item.Enabled, item.Order,item.Description));
                }
            }
            return pluginApplyConfigs;
        }

        public static PluginApplyConfigDto FromPluginApplyConfig(List<PluginApplyConfig> pluginApplyConfigs, IReadOnlyList<PluginType> pluginTypes, IEnumerable<ManifestWithConfig> manifestWithConfigs)
        {
            var configs = pluginApplyConfigs.GroupBy(c => new { c.ExecutableTypeFullName });
            var _configs = configs.Select(g => new PluginsTypeConfig
            {
                ExecutableTypeFullName = g.Key.ExecutableTypeFullName,
                Name = pluginTypes.FirstOrDefault(t => t.ExecutableTypeFullName == g.Key.ExecutableTypeFullName)?.Name,
                ConfigItems = g.Select(c =>
                {
                    var manifest = manifestWithConfigs.FirstOrDefault(m => m.ClassName == c.EntryFullName);
                    return new ConfigItem
                    {
                        Configurable = manifest?.Configurable,
                        PluginDescription = manifest?.Description,
                        Description = c.Description,
                        ConfigValue = c.ConfigValue,
                        Enabled = c.Enabled,
                        Id = c.Id,
                        Name = c.Name,
                        Order = c.Order,
                        EntryFullName = c.EntryFullName
                    };
                }).ToArray()
            })
                .ToArray();
            var result = new PluginApplyConfigDto();
            result.Configs = _configs;
            return result;
        }
    }
    /// <summary>
    /// 配置信息
    /// </summary>
    public class PluginsTypeConfig
    {
        /// <summary>
        /// 可执行插件类型名称（通常为类名全称）
        /// </summary>
        public string ExecutableTypeFullName { get; set; }
        /// <summary>
        /// 插件名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 配置项
        /// </summary>
        public ConfigItem[] ConfigItems { get; set; }

    }
    /// <summary>
    /// 配置项
    /// </summary>
    public class ConfigItem
    {
        /// <summary>
        /// 唯一标识（数据库标识）
        /// </summary>
        public long Id { get; set; }
        /// <summary>
        /// 全称
        /// </summary>
        public string EntryFullName { get; set; }
        /// <summary>
        /// 配置名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 配置值
        /// </summary>
        public string ConfigValue { get; set; }
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; }
        /// <summary>
        /// 排序
        /// </summary>
        public int Order { get; set; }
        /// <summary>
        /// 是否可配置
        /// </summary>
        public bool? Configurable { get; set; }
        /// <summary>
        /// 描述（配置自定义的描述）
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 插件描述（插件类自带的描述）
        /// </summary>
        public string PluginDescription { get; set; }
    }
}
